package com.stars.common;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则学习 https://blog.csdn.net/qq_28633249/article/details/77686976
 * 匹配中文:[\u4e00-\u9fa5]  英文字母:[a-zA-Z]  数字:[0-9]
 * 只含有汉字、数字、字母、下划线，下划线位置不限：
 * (^[a-zA-Z0-9_\u4e00-\u9fa5]+$)
 *
 * @author csot.qhluodajie on 2019/1/7.
 *         字符正则验证
 */
public class MyStrRegex {
    /**
     * 校验表名,正则
     * (?!_)　　不能以_开头
     * (?!.*?_$)　不能以_结尾
     * [a-z-_] 必须是小写+_，
     * {5,20} 长度
     */
    public static final String TABLE_NAME_REGEX = "^(?!_)(?!.*?_$)[a-z-_]{1,20}$";
    public static final String FIELD_NAME_REGEX = "^(?!_)(?!.*?_$)(?!0-9)[a-z-_0-9]{1,20}$";
    public static final String MODULE_NAME = "[a-z]{1,20}$";

    public static boolean strValidRegex(String str, String regex) {
        return str.matches(regex);
    }

    public static void main(String[] args) {
//        String str = "wwe_wewwe_Eew";
//        String regex = "^(?!_)(?!.*?_$)[a-z-_]$";
//        System.out.println("_____________________正则__________________");
//        System.out.println(str.matches(regex));
//        System.out.println("-----------------下划线后字母大写---------------------------");
//        System.out.println(underLineBackFirstCharToUpperCase(str));
//        System.out.println("----------------------------------------------------------");
//        String line = "sdsdWdsdSee";
//        System.out.println(upperCharToUnderLine(line));
        String name = "com.sccbv.entity";
        String packageName = name.replace(".","/")+"/";
        System.out.println("----------------------------------------------------------");
        System.out.println(packageName);
    }

    /**
     * 字母有下划线后第一个字母大写
     *
     * @param code
     * @return
     */
    public static String underLineBackFirstCharToUpperCase(String code) {
        char[] charArray = code.toCharArray();
        // A-Z 对应数字65-90 a-z 对应数字97-122
        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == '_') {
                // 字符在97-122之间的都是小写字母，在原基础上加32转换成大写
                if (charArray[i + 1] >= 97 && charArray[i + 1] <= 122) {
                    int upper = charArray[i + 1] - 32;
                    charArray[i + 1] = (char) upper;
                }
            }
        }
        StringBuffer url = new StringBuffer("");
        for (int i = 0; i < charArray.length; i++) {
            url.append(charArray[i]);
        }
        return url.toString().replace("_", "");
    }

    /**
     * 把字符串第一个字母大写
     * @param fildeName
     * @return
     */
    public static String fistNameToUpperCase(String fildeName){
         byte[] items = fildeName.getBytes();
         items[0] = (byte)((char)items[0]-'a'+'A');
        return new String(items);
    }

    /**
     * 把字符串第一个字母小写
     * @param str
     * @return
     */
    public static String fistNameToLowerCase(String str){
        byte[] items = str.getBytes();
        items[0] = (byte)((char)items[0]-'A'+'a');
        return new String(items);
    }

    /**
     * 字符串匹配将大写字符改为小写加下划线
     * @param param
     * @return
     */
    public static String upperCharToUnderLine(String param) {
        String regex = "[A-Z]";
        Pattern p = Pattern.compile(regex);
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }
        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
    /**
     * @Description: 通过反射拿对象属性的值
     * @Param: [map, obj] map <key(属性)，value(查询方式)>
     * @return: java.lang.String
     * @Author: dj
     * @Date: 2019/10/31
     */
    public static String sqlWhere(Map<String,String> map,Object obj){
        StringBuilder whereParams = new StringBuilder();
        map.forEach((k,v)->{
            String param = (String) MyReflectionUtils.getFieldValue(obj, k);
            if(StringUtils.isNotBlank(param)){
                whereParams.append("  AND  "+MyStrRegex.upperCharToUnderLine(k)+v+"'"+param+"'  ");
            }
        });
        String param = whereParams.toString();
        if(StringUtils.isBlank(param)){
            param = " AND 1=1  ";
        }
        return param;
    }


}
